"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
exports.__esModule = true;
require("reflect-metadata");
var typeorm_1 = require("typeorm");
var typeorm_2 = require("typeorm");
/**
 * 数据实体（表）
 */
var city = (function () {
    function city() {
    }
    __decorate([
        typeorm_1.PrimaryColumn("int", { length: 11 })
    ], city.prototype, "id");
    __decorate([
        typeorm_1.Column("char", { length: 35 })
    ], city.prototype, "name");
    __decorate([
        typeorm_1.Column("char", { length: 3 })
    ], city.prototype, "countryCode");
    __decorate([
        typeorm_1.Column("char", { length: 20 })
    ], city.prototype, "district");
    __decorate([
        typeorm_1.Column("int", { length: 11 })
    ], city.prototype, "population");
    city = __decorate([
        typeorm_1.Entity()
    ], city);
    return city;
}());
exports.city = city;
/**
 * 连接配置（MySQL）
 */
var conn = {
    type: "mysql",
    host: "localhost",
    port: 3306,
    database: "world",
    username: "root",
    password: "root",
    entities: [
        city
    ],
    autoSchemaSync: false
};
// /**
//  * 创建数据连接
//  * @remark 通过ID查找
//  */
// createConnection(conn).then(connection => {
//     let cityRepository = connection.getRepository(city);
//     console.log("---------- City Repository ----------");
//     console.log(cityRepository);
//     console.log("-------------------------------------");
//     cityRepository.findOneById(1)
//         .then(value => {
//             console.log("---------  City by id 1 -------------");
//             console.log(value);
//             console.log("-------------------------------------");
//         }, error => console.log(error));
// }).catch(error => console.log(error.message));
// createConnection(conn).then(connection => {
//     let cityRepository = connection.getRepository(city);
//     let insertCity = new city();
//     insertCity.name = "Foshan[test]";
//     insertCity.district = "Foshan[test]";
//     insertCity.countryCode = "CHN";
//     insertCity.population = 342342;
//     cityRepository
//         .save(insertCity)
//         .then(result => {
//             console.log(`${result.name} 添加成功`)
//         }, error => console.log(error.message))
// }, error => console.log(error.message));
// /**
//  * 修改
//  */
// createConnection(conn).then(connection => {
//     let cityRepository = connection.getRepository(city);
//     cityRepository.findOneById(4082)
//         .then(queryCity => {
//             if (queryCity) {
//                 console.log("---------  City by id 4081 -------------");
//                 console.log(queryCity.name);
//                 console.log("-------------------------------------");
//                 queryCity.name = "Foshan[update]";
//                 cityRepository.save(queryCity)
//                     .then(result => {
//                         console.log("---------- update City -------------");
//                     }, error => console.log(error.message));
//             }
//         }, error => console.log(error.message));
// }).catch(error => console.log(error.message));
// /**
//  * 删除
//  */
// createConnection(conn).then(connection => {
//     let cityRepository = connection.getRepository(city);
//     cityRepository.findOneById(4083)
//         .then(queryCity => {
//             if (queryCity) {
//                 cityRepository.remove(queryCity)
//                     .then(result => {
//                         console.log("---------- remove City -------------");
//                     }, error => console.log(error.message));
//             }
//         }, error => console.log(error.message));
// }).catch(error => console.log(error.message));
// /**
//  * 通过sql查询
//  */
// createConnection(conn).then(connection => {
//     connection.query("SELECT * FROM world.country where IndepYear > 1993")
//         .then(value => {
//             console.log("---------  execute sql -------------");
//             console.log(value);
//             console.log("-------------------------------------");
//         }, error => console.log(error.message));
// }, error => console.log(error.message));
var manager = new typeorm_2.ConnectionManager();
var connect = manager.create(conn);
var connectPromise = connect.connect();
connectPromise.then(function (connection) {
    return connection.query("SELECT * FROM world.country");
})
    .then(function (value) {
    console.log("---------  execute sql IndepYear > 1989 -------------");
    console.log("value.length : " + value.length);
    console.log("-------------------------------------");
    console.log(value.slice(0, 5));
    console.log("-------------------------------------");
    connect.close();
})["catch"](function (error) { return console.log(error.message); });
// console.log("-------------  end  ---------------"); 
